class SHA256 {
    async Sha256(message: string): Promise<string> {
        return new Promise(async (resolve) => {
            // 将消息编码为 ArrayBuffer
            const encoder = new TextEncoder();
            const data = encoder.encode(message);

            // 使用 Web Crypto API 的 SubtleCrypto 接口进行 SHA-256 散列
            const hashBuffer = await crypto.subtle.digest('SHA-256', data);

            // 将 ArrayBuffer 转换为十六进制字符串
            const hashArray = Array.from(new Uint8Array(hashBuffer));
            const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join('');
            resolve(hashHex)
        })
    }
}
const Instance: SHA256 =  new SHA256();
export default Instance;

/*
// 示例用法
const message: string = 'yourMessage';
sha256(message)
    .then(hash => {
        console.log('SHA-256 Hash:', hash);
    })
    .catch(error => {
        console.error('Error:', error);
    });
*/
